#include "dxMenu.h"
#include ".\dxPlane.h"
#include ".\dxInput.h"
#include ".\dxSound.h"
#include ".\dxUtility.h"
#include ".\Common\dxDirectives.h"
#include ".\dxGameMgr.h"
using namespace std;

static int status;

dxMenu::dxMenu()
{
	myInput = NULL;
	sounds = NULL;
	background = NULL;
	continueGame = NULL;
	continueGameHighlighted = NULL;
	newGame = NULL;
	newGameHighlighted = NULL;
	exit = NULL;
	exitHighlighted = NULL;
	menuContinue = false;
	menuNew = true;
	menuExit = false;
	menuLose = false;
	menuWon = false;
	status = 1;
}

dxMenu::~dxMenu()
{
	if (myInput != NULL){delete myInput;}
	//if (sounds != NULL){delete sounds;}
	if (background != NULL){delete background;}
	if (continueGame != NULL){delete continueGame;}
	if (continueGameHighlighted != NULL){delete continueGameHighlighted;}
	if (newGame != NULL){delete newGame;}
	if (newGameHighlighted != NULL){delete newGameHighlighted;}
	if (exit != NULL){delete exit;}
	if (exitHighlighted != NULL){delete exitHighlighted;}
}

bool dxMenu::init(dxInput* input, LPDIRECT3DDEVICE9 device)
{
	myInput = input;
	menuItemSelected = 0;
	message = 0;
	
	sounds = new dxSound();
	//sounds->init(hWnd);

	//load background menubackground.jpg
	background = new dxPlane();
	background->loadPlane(device, CUtility::GetTheCurrentDirectory()+"/data/menubackground.jpg", true);

	//load lose image
	loseMenu = new dxPlane();
	loseMenu->loadPlane(device, CUtility::GetTheCurrentDirectory()+"/data/lose.jpg", true);

	//load won image
	wonMenu = new dxPlane();
	wonMenu->loadPlane(device, CUtility::GetTheCurrentDirectory()+"/data/won.jpg", true);
	
	//load continue game image
	continueGame = new dxPlane();
	continueGame->loadPlane(device, CUtility::GetTheCurrentDirectory()+"/data/continue.jpg", false);
	continueGame->setPosition(200,200);
	continueGameHighlighted = new dxPlane();
	continueGameHighlighted->loadPlane(device, CUtility::GetTheCurrentDirectory()+"/data/continueh.jpg", false);
	continueGameHighlighted->setPosition(200,200);

	//load new game image
	newGame = new dxPlane();
	newGame->loadPlane(device, CUtility::GetTheCurrentDirectory()+"/data/newgame.jpg", false);
	newGame->setPosition(200,300);
	newGameHighlighted = new dxPlane();
	newGameHighlighted->loadPlane(device, CUtility::GetTheCurrentDirectory()+"/data/newgameh.jpg", false);
	newGameHighlighted->setPosition(200,300);

	//load exit image
	exit = new dxPlane();
	exit->loadPlane(device, CUtility::GetTheCurrentDirectory()+"/data/exit.jpg", false); 
	exit->setPosition(200,400);
	exitHighlighted = new dxPlane();
	exitHighlighted->loadPlane(device, CUtility::GetTheCurrentDirectory()+"/data/exith.jpg", false); 
	exitHighlighted->setPosition(200,400);

	return true;
}

void dxMenu::update()
{
	myInput->getInput();
	
	if (myInput->keyPress(DIK_DOWN))
	{
		if (menuNew == true)
		{			
					menuExit = true;
					menuContinue = false;
					menuNew = false;
					status = 2;
					menuItemSelected = 1;
		} 
		else 
			if(menuExit == true) 
			{		
					menuExit = false;
					menuContinue = true;
					menuNew = false;
					status = 3;
					menuItemSelected = 2;
			}
		else 
			if(menuContinue == true)
			{			
					menuExit = false;
					menuContinue = false;
					menuNew = true;
					status = 1;
					menuItemSelected = 0;			
			}
	}

	if (myInput->keyPress(DIK_UP))
	{
		if (menuNew == true)
		{
					menuExit = false;
					menuContinue = true;
					menuNew = false;
					status = 3;
					menuItemSelected = 2;
		} 
		else 
			if(menuExit == true) 
			{
					menuExit = false;
					menuContinue = false;
					menuNew = true;
					status = 1;
					menuItemSelected = 0;
			}
		else 
			if(menuContinue == true)
			{
					menuExit = true;
					menuContinue = true;
					menuNew = false;
					status = 2;
					menuItemSelected = 1;
			}
	}




	if (myInput->keyPress(DIK_RETURN))
	{
		switch (menuItemSelected)
		{
			case 0: {message = NEW_GAME;
				sounds->Mp3Play();
								
				break;}
			case 1: {message = EXIT;
					
				break;}
			case 2: {message = CONTINUE;
				
				sounds->Mp3Play();
				break;}
		}	

	}
}

void dxMenu::render(LPDIRECT3DDEVICE9 device)
{
	if(!menuLose && !menuWon)
	{
		background->render(device);
		
		if (menuNew == true)
		{
			newGameHighlighted->render(device);
			continueGame->render(device);
			exit->render(device);
		} 
		else if(menuExit == true)
		{
			newGame->render(device);
			continueGame->render(device);
			exitHighlighted->render(device);
		}
		else if(menuContinue == true)
		{
			newGame->render(device);
			continueGameHighlighted->render(device);
			exit->render(device);
		}
	}
	else if(menuLose)
	{	
		sounds->Mp3Stop();
		loseMenu->render(device);
	}
	else 
	{	
		sounds->Mp3Stop();
		wonMenu->render(device);
	}
	
}

int dxMenu::getMessage()
{
	return message;
}